# -*- coding:utf-8 -*-
"""
Created on 31/lug/2011

@author: Luigi Poderico
"""


class Serializer:
    
    def __init__(self, aFile):
        self.theFile = aFile
        pass
    
    def __writeln(self, aStr):
        self.theFile.write("%s\n"%aStr)
        pass
    
    def serialize(self, anInstance):
        self.__resources(anInstance.theResources)
        self.__machines(anInstance.theMachines)
        self.__services(anInstance.theServices)
        self.__processes(anInstance.theProcesses)
        self.__balanceObjectives(anInstance.theBalanceObjectives)
        self.__weights(anInstance)
        pass
    
    def __resources(self, aResources):
        self.__writeln("%d"%len(aResources))
        for myResource in aResources:
            self.__writeln("%d"%myResource.theTransientFlag)
            self.__writeln("%d"%myResource.theWeightLoadCost)
            pass
        pass
    
    def __machines(self, aMachines):
        self.__writeln("%d"%len(aMachines))
        for myMachine in aMachines:
            self.__writeln("%d"%myMachine.theNeighborhoodId)
            self.__writeln("%d"%myMachine.theLocationId)
            self.__writeln(" ".join([str(x) for x in myMachine.theCapacities]))
            self.__writeln(" ".join([str(x) for x in myMachine.theSafetyCapacties]))
            self.__writeln(" ".join([str(x) for x in myMachine.theMachineMoveCost]))
            pass
        pass
    
    def __services(self, aServices):
        self.__writeln("%d"%len(aServices))
        for myService in aServices:
            self.__writeln("%d"%myService.theSpreadMin)
            self.__writeln("%d"%len(myService.theDependingServices))
            self.__writeln(" ".join([str(x) for x in myService.theDependingServices]))
            pass
        pass
    
    def __processes(self, aProcesses):
        self.__writeln("%d"%len(aProcesses))
        for myProcess in aProcesses:
            self.__writeln("%s"%myProcess.theServiceId)
            self.__writeln(" ".join([str(x) for x in myProcess.theRequirements]))
            self.__writeln("%s"%myProcess.theProcessMovementCost)
        pass
    
    def __balanceObjectives(self, aBalObjs):
        self.__writeln("%d"%len(aBalObjs))
        for myBalanceObj in aBalObjs:
            self.__writeln(" ".join(str(x) for x in [myBalanceObj.theResourceJ,
                                     myBalanceObj.theResourceK,
                                     myBalanceObj.theTarget]))
            self.__writeln("%s"%myBalanceObj.theWeightBalanceCost)
            pass
        pass
    
    def __weights(self, anInstance):
        self.__writeln("%d"%anInstance.theWeightProcessMoveCost)
        self.__writeln("%d"%anInstance.theWeightServiceMoveCost)
        self.__writeln("%d"%anInstance.theWeightMachineMoveCost)
        pass
    
    
    pass #class
